package city.spring.modules.work.controller;

import city.spring.configure.OperationLog;
import city.spring.modules.work.entity.WorkShiftEntity;
import city.spring.modules.work.service.WorkShiftService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：上班班次
 *
 * @author 侯坤林
 * @date 2020-06-04 16:43:43
 */
@RestController
@RequestMapping("/work_shift")
public class WorkShiftController extends ApiController {
    private final WorkShiftService workShiftService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<WorkShiftEntity, ?>> entityOrderFields;

    public WorkShiftController(WorkShiftService workShiftService) {
        this.workShiftService = workShiftService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(WorkShiftEntity::getId);
        entityOrderFields.add(WorkShiftEntity::getTitle);
        entityOrderFields.add(WorkShiftEntity::getStartTime);
        entityOrderFields.add(WorkShiftEntity::getEndTime);
        entityOrderFields.add(WorkShiftEntity::getVersion);
        entityOrderFields.add(WorkShiftEntity::getGmtCreated);
        entityOrderFields.add(WorkShiftEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>上班班次</strong> 列表
     */
    @OperationLog(value = "获取全部上班班次列表", type = "排班")
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, WorkShiftEntity entity) {
        LambdaQueryChainWrapper<WorkShiftEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>上班班次</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @OperationLog(value = "获取上班班次列表", type = "排班")
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, WorkShiftEntity entity) {
        LambdaQueryChainWrapper<WorkShiftEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<WorkShiftEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>上班班次</strong>
     *
     * @param id 主键ID
     */
    @OperationLog(value = "获取一个上班班次信息：#{result?.data?.title?:'未找到'}", type = "排班")
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(workShiftService.getById(id));
    }

    /**
     * 添加一个 <strong>上班班次</strong>
     *
     * @param entity 修改后的信息
     */
    @OperationLog(value = "保存一个上班班次信息:#{#entity.title}", type = "排班")
    @PostMapping
    public Object add(@RequestBody WorkShiftEntity entity) {
        workShiftService.saveWorkShift(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>上班班次</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @OperationLog(value = "修改一个上班班次信息:#{#entity.title}", type = "排班")
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody WorkShiftEntity entity) {
        entity.setId(id);
        workShiftService.updateWorkShift(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>上班班次</strong>
     *
     * @param id 主键ID
     */
    @OperationLog(value = "删除一个上班班次信息:#{#id}", type = "排班")
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        workShiftService.deleteWorkShift(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>上班班次</strong>
     *
     * @param ids 主键ID列表
     */
    @OperationLog(value = "删除多个上班班次信息:#{#ids}", type = "排班")
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        workShiftService.deleteWorkShift(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<WorkShiftEntity> buildLambdaQuery(WorkShiftEntity entity) {
        LambdaQueryChainWrapper<WorkShiftEntity> lambdaQuery = workShiftService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), WorkShiftEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTitle()), WorkShiftEntity::getTitle, entity.getTitle());
        lambdaQuery.eq(entity.getStartTime() != null, WorkShiftEntity::getStartTime, entity.getStartTime());
        lambdaQuery.eq(entity.getEndTime() != null, WorkShiftEntity::getEndTime, entity.getEndTime());
        lambdaQuery.eq(entity.getVersion() != null, WorkShiftEntity::getVersion, entity.getVersion());
        lambdaQuery.eq(entity.getGmtCreated() != null, WorkShiftEntity::getGmtCreated, entity.getGmtCreated());
        lambdaQuery.eq(entity.getGmtModified() != null, WorkShiftEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

}
